package com.zaofan.controller;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zaofan.bean.AppyLog;
import com.zaofan.bean.AppyOpen;
import com.zaofan.constant.StateConstant;
import com.zaofan.service.AppyOpenService;
import com.zaofan.utils.JsonUtils;
import com.zaofan.utils.WebUtils;

@WebServlet("/appy")
public class AppyOpenController extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private AppyOpenService appyOpenService = new AppyOpenService();

	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Map<String, Object> params = new HashMap<>();
			Enumeration<String> enumeration = request.getParameterNames();
			while (enumeration.hasMoreElements()) {
				String name = enumeration.nextElement();
				if ("action".equals(name)) {
					continue;
				}
				Object value = request.getParameter(name);
				params.put(name, value);
			}
			List<AppyOpen> appyOpens = appyOpenService.findAll(params);
			Long count = appyOpenService.count(params);
			JsonUtils.writeSuccessPageJson("查询成功", appyOpens, count, response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("参数错误!!", response);
		}
		JsonUtils.writeListFailJson("查询失败!!", response);
	}
	
	
	protected void appy(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Map<String, Object> params = new HashMap<>();
			Enumeration<String> enumeration = request.getParameterNames();
			while (enumeration.hasMoreElements()) {
				String name = enumeration.nextElement();
				if ("action".equals(name)) {
					continue;
				}
				Object value = request.getParameter(name);
				params.put(name, value);
			}
			List<AppyOpen> appyOpens = appyOpenService.findAll(params);
			Long count = appyOpenService.count(params);
			JsonUtils.writeSuccessPageJson("查询成功", appyOpens, count, response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("参数错误!!", response);
		}
		JsonUtils.writeListFailJson("查询失败!!", response);
	}
	
	protected void check(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Map<String, Object> params = new HashMap<>();
			Enumeration<String> enumeration = request.getParameterNames();
			while (enumeration.hasMoreElements()) {
				String name = enumeration.nextElement();
				if ("action".equals(name)) {
					continue;
				}
				Object value = request.getParameter(name);
				params.put(name, value);
			}
			params.put("state", StateConstant.APPY_STATE_CHECKED);
			List<AppyOpen> appyOpens = appyOpenService.findAll(params);
			Long count = appyOpenService.count(params);
			JsonUtils.writeSuccessPageJson("查询成功", appyOpens, count, response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("参数错误!!", response);
		}
		JsonUtils.writeListFailJson("查询失败!!", response);
	}


	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			AppyOpen appyOpen = WebUtils.write2bean(request, AppyOpen.class);
			appyOpen.setState(StateConstant.APPY_STATE_OK);
			appyOpenService.addAppy(appyOpen);
			JsonUtils.writeSuccessJson("添加成功", "", response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("添加失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeListFailJson("添加失败!!", response);
	}

	protected void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			AppyOpen appyOpen = WebUtils.write2bean(request, AppyOpen.class);
			appyOpenService.update(appyOpen);
			JsonUtils.writeSuccessJson("修改成功", "", response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("修改失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeListFailJson("修改失败!!", response);
	}

	protected void get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String id_s = request.getParameter("id");
			Integer id = Integer.valueOf(id_s);
			AppyOpen appyOpen = appyOpenService.findById(id);
			JsonUtils.writeSuccessJson("查询成功", appyOpen, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询成功!!" + e.getMessage(), response);
		}
		JsonUtils.writeBeanFailJson("查询成功!!", response);
	}

	
	protected void setState(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			AppyOpen appyOpen = WebUtils.write2bean(request, AppyOpen.class);
			appyOpenService.setState(appyOpen);
			JsonUtils.writeSuccessJson("修改成功", "", response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("修改失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeListFailJson("修改失败!!", response);
	}
	
	protected void logs(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String id_s = request.getParameter("id");
			List<AppyLog> logs = appyOpenService.findLogs(id_s);
			JsonUtils.writeSuccessJson("查询成功", logs, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeBeanFailJson("查询成功!!", response);
	}

}
